package com.sm.dao;

import com.sm.entity.UserInfo;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;
import org.springframework.stereotype.Repository;
import org.springframework.web.bind.annotation.PathVariable;

import java.util.List;

@Repository
public interface UserDao {

    @Select("select * from user")
    public List<UserInfo> findAll() throws Exception;

    /**
     * 查询所有用户信息 不包含密码 用于显示员工信息
     * @return 所有用户信息
     * @throws Exception 所有异常
     */
    @Select("select uid,username,name,email,phonenum,gender,date,status from user where status>0")
    @Results(id = "userMap",value = {
            @Result(id = true,property = "uid",column = "uid"),
            @Result(property = "username",column = "username"),
            @Result(property = "email",column = "email"),
            @Result(property = "phoneNum",column = "phonenum"),
            @Result(property = "gender",column = "gender"),
            @Result(property = "date",column = "date"),
            @Result(property = "status",column = "status"),
            @Result(property = "roles",column = "uid",javaType = java.util.List.class ,many = @Many(select = "com.sm.dao.RoleDao.findRoleByUid"))
    })
    public List<UserInfo> findAllUser() throws Exception;

    /**
     * 根据姓名查询用户信息 用于登录验证 需要密码
     * @param username 员工应用名
     * @return 返回员工全部信息
     * @throws Exception 所有异常
     */
    @Select("select * from user where username = #{username}")
    @ResultMap("userMap")
    public UserInfo findByUsername(String username) throws Exception;

    /**
     * 根据uid查询用户信息 用于修改用户信息
     * @param uid 员工ID
     * @return 返回待修改员工信息
     * @throws Exception 所有异常
     */
    @Select("select uid,username,name,email,phonenum,gender,date,status,baseSalary,wagesLevel from user where uid = #{uid}")
    @ResultMap("userMap")
    public UserInfo findByUid(Integer uid) throws Exception;

    /**
     * 根据uid查询用户信息 用于修改用户信息
     * @param uid 员工ID
     * @return 返回待修改员工信息
     * @throws Exception 所有异常
     */
    @Select("select uid,username,name,email,phonenum,gender,date,status,baseSalary,wagesLevel from user where uid = #{uid}")
    public UserInfo findUserAsWages(Integer uid) throws Exception;

    /**
     * @return 返回所有员工数量
     * @throws Exception 所有异常
     */
    @Select("select count(uid) from user where status > 0")
    public Integer findUserCount()throws Exception;

    /**
     * @return 返回所有待转正人员
     * @throws Exception 所有异常
     */
    @Select("select uid,username,name,email,phonenum,gender,date,status from user where status = 3")
    @ResultMap("userMap")
    public List<UserInfo> findInformalUser()throws Exception;

    //模糊查询    未转正员工姓名
//    @Select("select uid,username,name,email,phonenum,gender,date,status from user where name like CONCAT('%', #{name}, '%') and status = #{status}")
    @Select({"<script>",
            "SELECT uid,username,name,email,phonenum,gender,date,status FROM user",
            "WHERE name like CONCAT('%', #{name}, '%')",
            "<when test='status == 3'>",
            "AND status = 3 ",
            "</when>" +
            "<when test='status!=3'> AND status > 0 </when> ",
            "</script>"})
    @ResultMap("userMap")
    public List<UserInfo> findUserLikeName(@Param("name") String name,@Param("status")Integer status)throws Exception;

    //查询员工基本工资
    @Select("select baseSalary from user where uid = #{uid}")
    public Integer findBaseSalary(Integer uid)throws Exception;

    //根据用户名或者姓名查询uid
    @Select({"<script>",
            "SELECT uid FROM user",
            "WHERE 1 = 1",
            "<when test='username != null'>AND username like CONCAT('%', #{username})</when>",
            "<when test='name != null'>AND name like CONCAT('%', #{name}, '%')</when>",
            "</script>"})
    public List<Integer> findUidByUsernameOrName(@Param("username")String username,@Param("name")String name);

    /**
     * 添加员工 返回主键值 用于添加员工
     * @param userInfo 要添加的员工信息
     * @throws Exception 所有异常
     */
    @Options(useGeneratedKeys = true, keyProperty = "uid", keyColumn = "uid")
    @Insert("insert into user(username,name,password,email,phonenum,gender,date,status,baseSalary,wagesLevel) " +
            "values(#{username},#{name},#{password},#{email},#{phoneNum},#{gender},#{date},#{status},#{baseSalary},#{wagesLevel})")
    public void addUser(UserInfo userInfo)throws Exception;

    /**
     * 更新员工信息 用于修改员工信息
     * @param userInfo 员工信息
     */
    @Update("<script>" +
            "update user " +
            "<set> " +
            " <if test='username != null'>username = #{username,jdbcType=VARCHAR},</if> " +
            " <if test='name != null'>name = #{name,jdbcType=VARCHAR},</if> " +
            " <if test='password != null'>password = #{password,jdbcType=VARCHAR},</if> " +
            " <if test='phoneNum != null'>phonenum = #{phoneNum,jdbcType=VARCHAR},</if> " +
            " <if test='email != null'>email = #{email,jdbcType=VARCHAR},</if> " +
            " <if test='gender != null'>gender = #{gender,jdbcType=INTEGER},</if> " +
            " <if test='date != null'>date = #{date,jdbcType=DATE},</if> " +
            " </set> " +
            " where uid = #{uid,jdbcType=INTEGER}" +
            "</script>")
    public void updateUser(UserInfo userInfo) throws Exception;
//" <if test='status != 0'>status = #{status,jdbcType=INTEGER},</if> " +

    /**
     * 1正式员工 0离职员工 2待培训员工
     * 更新员工状态
     * @param uid 员工ID
     * @param status  要更新的状态
     * @throws Exception 所有异常
     */
    @Update("update user set status = #{status} where uid = #{uid}")
    public void updateUserStatus(@Param("uid")Integer uid,@Param("status")Integer status) throws Exception;

    //更新基本工资
    @Update("update user set baseSalary = #{baseSalary} where uid = #{uid}")
    public void updateBaseSalary(@Param("uid")Integer uid,@Param("baseSalary")Integer baseSalary)throws Exception;

    //更新基本工资 和 工资级别
    @Update("<script> update user " +
            "<set> " +
            " <if test='baseSalary != null'>baseSalary = #{baseSalary,jdbcType=DOUBLE},</if> " +
            " <if test='wagesLevel != null'>wagesLevel = #{wagesLevel,jdbcType=INTEGER},</if> " +
            " </set> " +
            " where uid = #{uid,jdbcType=INTEGER} </script>")
    public void updateSalaryOrLevel(UserInfo userInfo)throws Exception;

    /**
     * 更新用户状态
     * @param userInfo 员工信息
     * @throws Exception 所有异常
     */
    @Update("update user set status = 0 where uid = #{uid}")
    public void deleteUser(UserInfo userInfo) throws Exception;


    /**
     * 删除员工
     * @param userInfo 员工信息
     * @throws Exception 所有异常
     */
    @Delete("delete from user where uid = #{uid}")
    public void clearUser(UserInfo userInfo) throws Exception;
}
